# 强制所有文本元素用 Heiti TC
def force_chinese_font(ax):
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontfamily("Heiti TC")
    ax.xaxis.label.set_fontfamily("Heiti TC")
    ax.yaxis.label.set_fontfamily("Heiti TC")
    ax.title.set_fontfamily("Heiti TC")
    legend = ax.get_legend()
    if legend:
        legend.set_title(legend.get_title().get_text(), fontfamily="Heiti TC")
        for text in legend.get_texts():
            text.set_fontfamily("Heiti TC")
import matplotlib.pyplot as plt

def setup_chinese_font():
    import matplotlib
    font_list = ["Heiti TC", "sans-serif"]
    matplotlib.rcParams['font.sans-serif'] = font_list
    matplotlib.rcParams['font.family'] = font_list
    matplotlib.rcParams['axes.unicode_minus'] = False
    plt.rcParams['font.sans-serif'] = font_list
    plt.rcParams['font.family'] = font_list
    plt.rcParams['axes.unicode_minus'] = False
    print("字体配置已加载：", plt.rcParams["font.sans-serif"])


# -*- coding: utf-8 -*-
import matplotlib
def set_plot_style(style_cfg):
    """
    Set matplotlib/seaborn style and Chinese font for Mac/Windows.
    Args:
        style_cfg: dict, e.g. from config.yaml['output']['figures']
    """
    matplotlib.rcParams['font.sans-serif'] = ['Heiti TC']
    matplotlib.rcParams['axes.unicode_minus'] = False
    import seaborn as sns
    plt_style = style_cfg.get('style', 'darkgrid')
    sns.set_style(plt_style)
    import matplotlib.pyplot as plt
    plt.rcParams['figure.dpi'] = style_cfg.get('dpi', 300)
    plt.rcParams['figure.figsize'] = style_cfg.get('figsize', [12, 8])
    plt.rcParams['savefig.bbox'] = style_cfg.get('bbox_inches', 'tight')
